package com.xiaoy.core.base.dictionary.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xiaoy.core.base.dictionary.dao.CityDao;
import com.xiaoy.core.base.dictionary.dao.ProvinceDao;
import com.xiaoy.core.base.dictionary.entity.Area;
import com.xiaoy.core.base.dictionary.entity.City;
import com.xiaoy.core.base.dictionary.entity.Province;
import com.xiaoy.page.PageWrapper;
import com.xiaoy.util.format.FileUtil;

@Service("dictionaryService")
public class DictionaryService {
	
	
	public List<String> getValueByCode (String code){
		
		List<String> list = DictionaryFileService.getValue(code);
		if (list == null) {
			
		}
		return list;
	}
	
	public PageWrapper filterPage (PageWrapper page) {
		
		Map<String,List<String>> store = new HashMap<String, List<String>>();
		store.putAll(DictionaryFileService.getAllValue());
		page.setResultData(store);
		return page;
	}
	
	@Resource
	private ProvinceDao provinceDao;
	@Resource
	private CityDao cityDao;
	
	
	public String createProvinceCtiyJs () throws IOException {
		
		StringBuffer sb = new StringBuffer("var Addr = {}; ");
		
		List<Province> provinces = provinceDao.getAll();
		
		sb.append("Addr.provinces = [");
		for (Province p : provinces){
			sb.append("{id:'"+p.getId()+"',name:'"+p.getName()+"',code:'"+p.getCode()+"',citys:[");
			
			for (City c:p.getCitys()) {
				sb.append("{id:'"+c.getId()+"',name:'"+c.getName()+"',code:'"+c.getCode()+"',areas:[");
				for (Area a : c.getAreas()) {
					sb.append("{id:'"+a.getId()+"',name:'"+a.getName()+"',code:'"+a.getCode()+"'},");
				}
				sb.append("]},");
			}
			sb.append("]},");
		}
		sb.append("]");
		String result = sb.toString();
		
		;
		
		String s = FileUtil.getPath("com");
		System.out.println("getClassLoader-->"+s);
		FileUtil.wirte("/"+s+"/../../../base/js", "addrData.js", result);
		System.out.println("DictionaryService.createProvinceCtiyJs-->\n"+result);
		return result;
	}
	
	
	
}
